package com.mobi.cacti.ns.view;

import java.util.ArrayList;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.app.Fragment;
import org.holoeverywhere.widget.ListView;
import org.holoeverywhere.widget.ProgressBar;

import com.mobi.cacti.R;
import com.mobi.cacti.ns.model.ListItem;
import com.mobi.cacti.ns.view.adapter.ItemListAdapter;
import com.mobi.cacti.ns.view.tab.BaseContainerFragment;
import com.mobi.cacti.rabbit.controllers.TicketCountActivityController;
import com.mobi.cacti.rabbit.controllers.TicketCountActivityControllerCallback;
import com.mobi.cacti.rabbit.response.Cache;
import com.mobi.cacti.rabbit.response.ReqTicketCount;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class TicketCircuitListActivity extends Fragment implements TicketCountActivityControllerCallback {

	private TicketCountActivityController ticketCountActivityController;
	
	private ReqTicketCount[] ticketCountArray;
	private ArrayList<ListItem> items;
	
	private ItemListAdapter adapter;
	
	private ProgressBar progressBar;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		ticketCountActivityController = TicketCountActivityController.getInstance();
		ticketCountActivityController.delegate = this;
		
		items = new ArrayList<ListItem>();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		items.clear();
		
		View v = LayoutInflater.from(getActivity()).inflate(
				R.layout.ticketcircuitlist_layout, null);

		ticketCountActivityController.loadTicketCount(this.getActivity());
		
		progressBar = (ProgressBar) v.findViewById(R.id.progressbar);
		
		adapter = new ItemListAdapter(getSupportActivity(),
				R.layout.rowitem_layout, items);
		ListView listView = (ListView) v.findViewById(R.id.ticketcircuitlist);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// init cache
				Cache.ticketListStateBundle = new Bundle();
				
				String circuitId = ticketCountArray[position].curcitId;
				
				Bundle bundle = new Bundle();
				bundle.putString("circuitId", circuitId);
				
				TicketListActivity ticketListActivity = new TicketListActivity();
				ticketListActivity.setArguments(bundle);
				
				((BaseContainerFragment)getParentFragment()).replaceFragment(ticketListActivity, true);
			}
		});

		return v;
	}

	@Override
	public void ticketCountActivityControllerCompleteCountTicket() {
		progressBar.setVisibility(View.GONE);
		
		items.clear();
		ticketCountArray = Cache.ticketCountArray;

		for (ReqTicketCount reqTicketCount : ticketCountArray) {
			ListItem item = new ListItem();
			item.label = "Circuit No. " + reqTicketCount.curcitId;
			item.ticketCountLabel = (reqTicketCount.ticketCount == 0) ? null : reqTicketCount.ticketCount + "";
			item.isShowRightArrow = true;
			items.add(item);
		}
		
		adapter.notifyDataSetChanged();
	}


}
